﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using WebChart;
using Cotrobaie.UsersWebService;
using System.Drawing.Drawing2D;

namespace Cotrobaie.UserControl
{
    public partial class Statistici : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Server.UrlDecode(Request.Params["ProductID"]) == null) { Response.Write("Trebuie un ProductID"); return; }

            this.Response.Clear();
            //this.Response.ContentType = "text";

            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                var preturi = serviciu.GetProductPrice(long.Parse(Server.UrlDecode(Request.Params["ProductID"])));
                if (preturi.Success)
                {
                    var Prices = from i in (from p in preturi.Produse select p) group i by i.SiteID into h select h;
                    this.Response.Write(CreateChart(Prices));
                }
            }
        }
        private string CreateChart(System.Collections.Generic.IEnumerable<IGrouping<int, UsersWebService.TipPreturi>> Prices)
        {
            chartControl1.Charts.Clear();
            //culori compatibile cu designul site-ului
            Color[] c = new Color[] { Color.Coral, Color.Black, Color.Black, Color.Green, Color.GreenYellow, Color.Gray };
            byte index = 0;

            double min = Double.MaxValue, max = Double.MinValue;
            int maxCount = 0;
            foreach (var v in Prices)
                if (v.Count() > maxCount) maxCount = v.Count();
            chartControl1.XTicksInterval = 5;
            chartControl1.YValuesInterval = 5;
            chartControl1.XValuesInterval = (maxCount + 1) / 2; //afisez primu si ultimu

            foreach (var p in Prices.Reverse())
            {
                LineChart chart = new LineChart();

                chart.Line.Width = 2.7F;
                chart.Line.LineJoin = LineJoin.Round;
                chart.Line.Color = c[index];
                chart.Fill.Color = c[index];
                chart.LineMarker = new DiamondLineMarker(2, c[index], c[index++]);
                chart.Shadow.Visible = false;
                chart.DataXValueField = "Data";
                if (index == c.Count()) index = 0;//daca vom avea mai multi produceri

                var pret = p.OrderBy(x => x.Data);
                chart.Legend = pret.First().SiteName;
                if (pret.Min(x => x.Pret).Value < min) min = pret.Min(x => x.Pret).Value;
                if (pret.Max(x => x.Pret).Value > max) max = pret.Max(x => x.Pret).Value;

                int count = 0;
                foreach (var pr in pret)
                { chart.Data.Add(new ChartPoint(pr.Data.ToString("dd.MM.yyyy"), (float)pr.Pret)); count++; }
                while (count < maxCount)
                { chart.Data.Add(new ChartPoint(pret.Last().Data.ToString("dd.MM.yyyy"), (float)pret.Last().Pret)); count++; }
                //chart.Data.Add(new ChartPoint(DateTime.Now.ToString("dd.MM.yyyy"), (float)pret.Last().Pret)); count++;

                chartControl1.Charts.Add(chart);
            }

            if (max == min) chartControl1.YValuesInterval = 1;
            else if (max - min < 10) chartControl1.YValuesInterval = (int)((max - min) / 1.2);
            else if (max - min > 30) chartControl1.YValuesInterval = (int)((max - min) / 3);
            chartControl1.YCustomStart = (float)min - 1;
            chartControl1.YCustomEnd = (float)max + 1;
            chartControl1.RedrawChart();
            return chartControl1.FileName;
        }
    }
}